﻿//javascript: void((function() {var element = document.createElement('script');element.id = 'tools-for-js';element.charset = 'utf-8',element.setAttribute('src', 'http://127.0.0.1/private/tools-for-js/support_paste_on_input.js?' + Date.parse(new Date()));document.body.appendChild(element);})())

function getClipboard()
{
	if (window.clipboardData)
	{
		return (window.clipboardData.getData('Text'));
	}
    else if (window.netscape)
	{
		try
		{
			netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		}
		catch (e)
		{
			alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
			return null ;
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		clip.getData(trans, clip.kGlobalClipboard);
		var str = new Object();
		var len = new Object();
		try
		{
			trans.getTransferData('text/unicode', str, len);
		}
		catch (error)
		{
			return null;
		}
		if (str)
		{
			if (Components.interfaces.nsISupportsWString)
			{
				strstr = str.value.QueryInterface(Components.interfaces.nsISupportsWString);
			}
			else if (Components.interfaces.nsISupportsString)
			{
				strstr = str.value.QueryInterface(Components.interfaces.nsISupportsString);
			}
			else
			{
				str = null;
			}
      }
      if (str)
	  {
		return (str.data.substring(0, len.value / 2));
      }
    }
    return null;
}

function setInputValue(inputs, value)
{
	for (var i = 0 ; i < inputs.length ; i++)
	{
		inputs[i].value = value ;
	}
}

function setValue(cell, value)
{
	setInputValue(cell.getElementsByTagName('INPUT'), value);
	
	if (cell.innerHTML == cell.innerText)
	{
		cell.innerHTML = value ;
	}
	
	//for specific system
	if (cell.obj && cell.obj.text)
	{
		cell.obj.text = value ;
	}
	if (cell.obj && cell.obj.value)
	{
		cell.obj.value = value ;
	}
}

function pasteClipboardData()
{
	if (this.tagName != 'INPUT' || this.parentNode.tagName != 'TD' || this.parentNode.parentNode.tagName != 'TR')
	{
		return true ;
	}
	
	var tdIndex = this.parentNode.cellIndex;
	var trIndex = this.parentNode.parentNode.rowIndex;
	
	var str = getClipboard();
	var len = str.split("\n");

	var trStr ;
	var tb = this.parentNode.parentNode.parentNode ;
	for(var i = 0; i < len.length - 1; ++i)
	{
		if (i + trIndex >= tb.rows.length)
		{
			continue ;
		}
		trStr = len[i].split(/\s+/);

		tr = tb.rows[i + trIndex] ;
		for(var j = 0; j < trStr.length ; ++j)
		{
			if (j + tdIndex >= tr.cells.length)
			{
				continue ;
			}
			cell = tr.cells[j + tdIndex] ;
			setValue(cell, trStr[j]) ;
		}
	}
    return false; 
}


function HackWindow(_window)
{
	for (var i = 0 ; i < _window.frames.length ; i++)
	{
		if(_window.frames[i].contentWindow)
		{
			HackWindow(_window.frames[i].contentWindow);
		}
		else
		{
			HackWindow(_window.frames[i]);
		}
	}
	
	HackInput(_window.document);	
}

function HackInput(doc)
{
	var inputs = doc.getElementsByTagName('INPUT');
	for(var i = 0 ; i < inputs.length; i++)
	{
		var input = inputs[i] ;
		input.onpaste = pasteClipboardData;
	}
}


function HackLogic()
{
	HackWindow(window);
}

HackLogic() ;

alert("Load finished.");